<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/3/12
 * Time: 9:36
 */
namespace app\api\controller;
use think\Db;
class Club extends Base {
    /**
     * 异地登录验证
     * @return string
     */
//    public function __construct()
//    {
//        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == 112){
//            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
//            exit;
//        }elseif($auth == 110){
//            echo json_encode(['status' => 302, 'msg' => $this->frost]);
//            exit;
//        }
//    }
    //我加入我创建的俱乐部
    public function index(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['m_user_id'] = $user_id;
        $where['uc.club_status'] = 1;
        $where['a.m_status'] = 2;
        $list = Db::table('up_club_member')->alias('a')
            ->join('up_club uc','uc.club_id = a.club_id','left')
            ->join('up_gold ug','ug.gold_id = uc.gold_id','left')
            ->where($where)
            ->limit($limit,$num)
            ->field('uc.club_id,uc.club_img,uc.club_name,uc.total_price,ug.gold_price')
            ->select();
        return  json(array('status'=>200,'msg'=>$list));
    }
    //我创建的俱乐部
    public function found(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $where['m_user_id'] = $user_id;
        $where['uc.club_status'] = 1;
        $where['a.m_type'] = 2;
        $list = Db::table('up_club_member')->alias('a')
            ->join('up_club uc','uc.club_id = a.club_id','left')
            ->join('up_gold ug','ug.gold_id = uc.gold_id','left')
            ->where($where)
            ->limit($limit,$num)
            ->field('uc.club_id,uc.club_img,uc.club_name,uc.total_price,ug.gold_price')
            ->select();
        return  json(array('status'=>200,'msg'=>$list));
    }
    //我创建的俱乐部
    public function delMyClub(){
        extract(input());
        $where['club_id'] = $club_id;
        $data = Db::table('up_club')
            ->where($where)
            ->field('club_user_id')
            ->find();
        if($data['club_user_id'] != $user_id){
            return  json(array('status'=>100,'msg'=>'您不是俱乐部创建人！'));
        }
        $res = Db::table('up_club')
            ->where($where)
            ->setField('club_status',2);
        if($res){
            return  json(array('status'=>200,'msg'=>'解散成功'));
        }else{
            return  json(array('status'=>100,'msg'=>'解散失败，请稍后重试'));
        }

    }
    //金币多少一人
    public function gold(){
        $list = Db::table('up_gold')
            ->select();
        return  json(array('status'=>200,'msg'=>$list));
    }

    //创建俱乐部
    public function addClub(){
        extract(input());
        //获取VIP等级
        $vip_num = $this->getVip($user_id);
        //查询这个用户创建了多少俱乐部
        $map['club_user_id'] = $user_id;
        $num = Db::table('up_club')->where($map)->count();
        if($num>$vip_num){//如果创建的俱乐部大于VIP等级的权限
            return  json(array('status'=>100,'msg'=>"VIP等级权限不够,不能创建更多俱乐部"));
        }
        $name = input('param.name');
        $notice = input('param.notice');
        $hair_time = input('param.hair_time');
        $gold_id = input('param.gold_id');
        //查询一个人送多少金币
        $gold_price = Db::table('up_gold')->where('gold_id',$gold_id)->value('gold_price');
        //查询这个人的账户金币数量
        $user_balance = Db::table('up_u_user')->where('user_id',$user_id)->value('user_balance');
        //俱乐部送出去的每个人金币的10倍大于账户金币才能创建俱乐部
        if(($gold_price*10)>$user_balance){
            return  json(array('status'=>100,'msg'=>"金币不足不能创建俱乐部"));
        }
//        Db::startTrans();
//
//        try {
//            //插入俱乐部表
//            $data['club_user_id'] = $user_id;
//            $data['club_name'] = $name;
//            $data['club_notice'] = $notice;
//            $data['club_time'] = $hair_time;
//            $data['gold_id'] = $gold_id;
//            $data['club_id'] = $this->uuid();;
//            $data['create_time'] = date('Y-m-d H:i:s',time());
//            Db::table('up_club')->insertGetId($data);
//
//            $groupid = $data['club_id'];
//            //插入俱乐部成员表
//            $data1['m_user_id'] = $user_id;
//            $data1['club_id'] = $groupid;
//            $data1['m_status'] = 2;
//            $data1['m_type'] = 2;
//            $data1['m_create_time'] = date('Y-m-d H:i:s',time());
//            Db::table('up_club_member')->insertGetId($data1);
//            //更新男性用户消费记录  todo
//            Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_consume',$gold_price*10);
//            $uids = "userId=".$user_id."&";
//            $this->create($uids,$groupid,$name);
//
//            $uidstr = "userId=".$user_id."&";
//            $this->joinin($uidstr,$groupid,$name);
//            return  json(array('status'=>200,'msg'=>"创建成功"));
//        } catch (\Exception $e) {
//            // 回滚事务
//            Db::rollback();
//            return  json(array('status'=>100,'msg'=>"创建失败"));
//        }
//        //插入俱乐部表
        $data['club_user_id'] = $user_id;
        $data['club_name'] = $name;
        $data['club_notice'] = $notice;
        $data['club_time'] = $hair_time;
        $data['gold_id'] = $gold_id;
        $data['club_id'] = $this->uuid();;
        $data['create_time'] = date('Y-m-d H:i:s',time());
         Db::table('up_club')->insertGetId($data);

        $groupid = $data['club_id'];
        //插入俱乐部成员表
        $data1['m_user_id'] = $user_id;
        $data1['club_id'] = $groupid;
        $data1['m_status'] = 2;
        $data1['m_type'] = 2;
        $data1['m_create_time'] = date('Y-m-d H:i:s',time());
        $res1 = Db::table('up_club_member')->insertGetId($data1);
        //更新男性用户消费记录  todo
        Db::table('up_u_user')->where('user_id',$user_id)->setInc('user_consume',$gold_price*10);
        if($res1){
            $uids = "userId=".$user_id."&";
            $this->create($uids,$groupid,$name);

            $uidstr = "userId=".$user_id."&";
             $this->joinin($uidstr,$groupid,$name);
            return  json(array('status'=>200,'msg'=>"创建成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"创建失败"));
        }


    }
    //更换背景图片
    public function updateImg(){
        extract(input());
        $club_id = input('param.club_id');
        $img  = input('param.img');
        $res = Db::table('up_club')->where('club_id',$club_id)->setField('club_img',$img);
        if($res){
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }
    //俱乐部背景图片
    public function clubImg(){
        extract(input());
        $club_id = input('param.club_id');
        $res = Db::table('up_club')->where('club_id',$club_id)->field('club_img')->find();
        if($res){
            return  json(array('status'=>200,'msg'=>$res));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }

    //俱乐部成员
    public function member(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $club_id = input('param.club_id');
        $where['a.m_status'] = 2;
        $where['a.club_id'] = $club_id;
        $where['a.m_type'] = 1;
        $res = Db::table('up_club_member')->alias('a')
             ->join('up_u_user uuu','uuu.user_id = a.m_user_id','left')
             ->where($where)
             ->limit($limit,$num)
            ->field('a.*,uuu.user_username,uuu.user_image,uuu.user_gender,uuu.user_id')
            ->select();
        foreach ($res as $key=>&$vo){
            if($vo['user_gender']==2){
                $vo['vip'] = 1;
            }else{
                $vo['vip'] = $this->getVip($vo['user_id']);
            }
        }
        return  json(array('status'=>200,'msg'=>$res));
    }
    //申请成员列表
    public function apply(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $club_id = input('param.club_id');
        $where['a.m_status'] = 1;
        $where['a.club_id'] = $club_id;
        $res = Db::table('up_club_member')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.m_user_id','left')
            ->where($where)
            ->limit($limit,$num)
            ->field('a.*,uuu.user_username,uuu.user_image')
            ->select();
        return  json(array('status'=>200,'msg'=>$res));
    }

    //同意，删除俱乐部成员
    public function agree(){
        extract(input());
        $type = input('param.type');
        $member_id = input('param.member_id');
        $info = Db::table('up_club_member')->alias('a')
            ->join('up_club uc','uc.club_id = a.club_id','left')
            ->where('a.member_id',$member_id)
            ->field('a.*,uc.club_name')
            ->find();
        if($type==1){//同意
        $res = Db::table('up_club_member')->where('member_id',$member_id)->setField('m_status',2);
            $data['n_user_id'] = $info['m_user_id'];
            $data['n_content'] = "您申请的".$info['club_name']."俱乐部已经通过申请";
//            $data['n_other_user_id'] = $user_id;
            $data['n_time'] = date('Y-m-d H:i:s',time());
            Db::table('up_news')->insertGetId($data);
            //推送
            $list1 = [];
            $v = (string)$info['m_user_id'];
            array_push($list1, $v);
            $code=2;
            $str = array('msg'=>"您申请的".$info['club_name']."俱乐部已经通过申请",'type'=>9,'club_id'=>$info['club_id']);
            $str1 = "您申请的".$info['club_name']."俱乐部已经通过申请";
            try{
                $this->Jpush1(1, $list1, $str1, $str,$code);
            }catch(\Exception $e){

            }
            //查询俱乐部的名称
            $uidstr = "userId=".$info['m_user_id']."&";
            $this->joinin($uidstr,$info['club_id'],$info['club_name']);
        }elseif ($type==2){//从俱乐部移除
        $res = Db::table('up_club_member')->where('member_id',$member_id)->delete();
            $data['n_user_id'] = $info['m_user_id'];
            $data['n_content'] = "您已经被管理员从".$info['club_name']."俱乐部移除";
//            $data['n_other_user_id'] = $user_id;
            $data['n_time'] = date('Y-m-d H:i:s',time());
            Db::table('up_news')->insertGetId($data);
            //推送
            $list1 = [];
            $v = (string)$info['m_user_id'];
            array_push($list1, $v);
            $code=2;
            $str = array('msg'=>"您已经被管理员从".$info['club_name']."俱乐部移除",'type'=>9,'club_id'=>$info['club_id']);
            $str1 ="您已经被管理员从".$info['club_name']."俱乐部移除";
            try{
                $this->Jpush1(1, $list1, $str1, $str,$code);
            }catch(\Exception $e){

            }
            $uidstr = "userId=".$info['m_user_id']."&";
            $this->getout($uidstr,$info['club_id']);

        }
        if($res){
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }
    //公告管理
    public function notice(){
        extract(input());
        $club_id = input('param.club_id');
        $notice = input('param.notice');
        $res = Db::table('up_club')->where('club_id',$club_id)->setField('club_notice',$notice);
        if($res){
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }
    //加入俱乐部列表
    public function clubList(){
        extract(input());
        $type = input('param.type');
        $keyword = input('param.keyword');
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $order = '';
        if($type==1){//最新
            $order = 'a.create_time desc';
        }elseif ($type==2){
            $order = 'a.total_price desc';
        }
        $where = [
            'a.club_status' => 1
        ];
        if($keyword!=''){
            $where['a.club_name'] = array('like',"%$keyword%");
        }
        $list = Db::table('up_club')->alias('a')
            ->join('up_gold ug','ug.gold_id = a.gold_id','left')
            ->where($where)
            ->field('a.club_id,a.club_img,a.club_name,a.total_price,ug.gold_price')
            ->limit($limit,$num)
            ->order($order)
            ->select();
//        $list = Db::table('up_club_member')->alias('a')
//            ->join('up_club uc','uc.club_id = a.club_id','left')
//            ->join('up_gold ug','ug.gold_id = uc.gold_id','left')
//            ->field('uc.club_id,uc.club_img,uc.club_name,uc.total_price,ug.gold_price')
//            ->select();
        return  json(array('status'=>200,'msg'=>$list));
    }
    //控制显示隐藏
    public function isShow(){
        extract(input());
        $club_id = input('param.club_id');
        $is_show = input('param.is_show');
        Db::table('up_club')->where('club_id',$club_id)->setField('is_show',$is_show);
        return  json(array('status'=>200,'msg'=>"成功"));
    }
    //俱乐部详情
    public function clubDetail(){
        extract(input());
        $club_id = input('param.club_id');
        $list = Db::table('up_club')->alias('a')
            ->join('up_gold ug','ug.gold_id = a.gold_id','left')
            ->join('up_u_user uuu','uuu.user_id = a.club_user_id','left')
            ->where('a.club_id',$club_id)
            ->field('uuu.user_username,ug.gold_price,a.*')
            ->find();
        $where['a.m_status'] = 2;
        $where['a.club_id'] = $list['club_id'];
        $res = Db::table('up_club_member')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.m_user_id','left')
            ->where($where)
            ->field('uuu.user_username,uuu.user_image, m_user_id')
            ->select();
        //查询这个人有没有加入这个俱乐部
        $map['club_id'] = $club_id;
        $map['m_user_id'] = $user_id;
        $result = Db::table('up_club_member')->where($map)->find();
        if($result['member_id']!=''){
            if($result['m_status']==2||$result['m_type']==2){
            $list['is_join'] = 2;//已经加入了
            }
            if($result['m_status']==1){
            $list['is_join'] = 3;//已经申请了
            }
        }else{
            $list['is_join'] = 1;//没有加入
        }
        $list['join'] = $res;
        $list['vip'] = $this->getVip($list['club_user_id']);
        return  json(array('status'=>200,'msg'=>$list));
    }

    //申请加入俱乐部
    public function joinClub(){
        extract(input());
        $club_id = input('param.club_id');
        $content = input('param.content');
        $data['m_user_id'] = $user_id;
        $data['club_id'] = $club_id;
        $data['m_content'] = $content;
        $data['m_create_time'] = date('Y-m-d H:i:s',time());
        $res = Db::table('up_club_member')->insertGetId($data);
        //查询这个用户的昵称
        $user_username = Db::table('up_u_user')->where('user_id',$user_id)->value('user_username');
        //申请加入俱乐部消息
        $info = Db::table('up_club')->where('club_id',$club_id)->find();
        $data1['n_user_id'] = $info['club_user_id'];
        $data1['n_content'] = $user_username."申请加入您的".$info['club_name'];
//            $data['n_other_user_id'] = $user_id;
        $data1['n_time'] = date('Y-m-d H:i:s',time());
        Db::table('up_news')->insertGetId($data1);


        if($res){
            //推送
            $list1 = [];
            $v = (string)$info['club_user_id'];
            array_push($list1, $v);
            $code=2;
            $str = array('msg'=>$user_username."申请加入您的".$info['club_name'],'type'=>9,'club_id'=>$club_id);
            $str1 = $user_username."申请加入您的".$info['club_name'];
            try{
                $this->Jpush1(1, $list1, $str1, $str,$code);
            }catch(\Exception $e){

            }
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }

    //消息
    public function message(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = ($page-1)*$num;
        $list = Db::table('up_news')->where('n_user_id',$user_id)->limit($limit,$num)->select();
        return  json(array('status'=>200,'msg'=>$list));
    }

    //查询这个俱乐部所有的成员
    public function listClub(){
        extract(input());
        $id = input('param.id');

    }
}